<template>
    <div class="app-container">

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:Category:add']">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="Category" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="类型名称" align="center" prop="categoryName" />

            <el-table-column label="创造时间" align="center" prop="createTime" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:Category:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:Category:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--   
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" /> -->

        <!-- 添加或修改岗位对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="分类名字" prop="categoryName">
                    <el-input v-model="form.categoryName" placeholder="请输入分类名字" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

import { getCategory, addCategory, updateCategory, delCategory } from "@/api/system/category";
export default {
    name: "Category",
    dicts: ['sys_normal_disable', 'sys_number'],
    data() {
        return {
            columnsToShow: ['city', 'address', 'views', 'openTime', 'price'],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位表格数据
            Category: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            Image: false,
            fileList: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                CategoryName: [
                    { required: true, message: "岗位名称不能为空", trigger: "blur" }
                ],
                CategoryCode: [
                    { required: true, message: "岗位编码不能为空", trigger: "blur" }
                ],
                CategorySort: [
                    { required: true, message: "岗位顺序不能为空", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        shouldShowColumn(column) {
            // 检查columnsToShow数组中是否包含当前列名，并确保至少有一条记录包含此字段的值。
            if (!this.columnsToShow.includes(column)) return false;

            for (let item of this.Category) {
                if (item[column]) return true;
            }
            return false;
        },
        /** 查询列表 */
        getList() {
            this.loading = true;
            getCategory().then(response => {

                this.Category = response.data;
                //   this.total = 999;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                categoryName: undefined,
            };
            this.resetForm("form");
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.CategoryId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加分类";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            console.log(row);
                this.form = row;
                this.open = true;
                this.title = "修改分类";
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.title == '修改分类') {
                        updateCategory(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCategory(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const categoryId = row.categoryId || this.ids;
            this.$modal.confirm('是否确认删除岗位编号为"' + categoryId + '"的数据项？').then(function () {
                return delCategory(categoryId);
            }).then(() => {
                this.getList();
            }).catch(() => { });
        },

    }
};
</script>